<?php
/**
 * Created by PhpStorm.
 * User: BlueIdeaLirui
 * Date: 2020/12/28
 * Time: 11:20
 */

namespace business\company\repositories;

class SupplierBoutiqueRepository
{
    /**
     * 获取某个时间后的供应商总数
     * @param $interval string 距今相差天数
     * @return int
     */
    public function getSupplierCountByDate($interval = null)
    {
        /** @var Connection $connection */
        $connection = \Yii::$app->get('db');

        $sql = "SELECT count(1) FROM b2b_company.uuc_supplier_boutique WHERE status = 2";
        if (!empty($interval)) {
            $sql .= " AND last_modified_time > :datetime";
            return $connection->createCommand($sql)
                ->bindValue(":datetime", date("Y-m-d", strtotime($interval . " day")))
                ->queryScalar();
        } else {
            return $connection->createCommand($sql)->queryScalar();
        }
    }

    /**
     * 从供应商表中获取变更的供应商id
     * @param $offset
     * @param $pageSize
     * @return array
     */
    public function getAllSupplierId($offset, $pageSize)
    {
        $sql = <<<sql
            SELECT supplier_id
            FROM b2b_company.uuc_supplier_boutique 
            WHERE supplier_id > :offset AND status = 2
            order by supplier_id ASC
            LIMIT :pageSize
sql;
        /** @var Connection $connection */
        $connection = \Yii::$app->get('db');
        $result = $connection->createCommand($sql)
            ->bindValue(':offset', $offset)
            ->bindValue(':pageSize', $pageSize)
            ->queryColumn();
        return $result;
    }

    /**
     * 获取某个时间后的精选供应商总数
     * @param $startDate
     * @param $endDate
     * @return mixed
     * @throws \yii\base\InvalidConfigException
     */
    public function getSupplierCountByTimeSlot($startDate, $endDate)
    {
        /** @var Connection $connection */
        $connection = \Yii::$app->get('db');

        $sql = "SELECT count(1) FROM b2b_company.uuc_supplier_boutique WHERE status = 2";
        $sql .= " AND last_modified_time > :startdatetime AND last_modified_time < :enddatetime";
        return $connection->createCommand($sql)
            ->bindValue(':startdatetime', $startDate)
            ->bindValue(':enddatetime', $endDate)
            ->queryScalar();
    }

    /**
     * 从供应商精选库表中获取变更的供应商id
     * @param $startDate
     * @param $endDate
     * @param $offset
     * @param $pageSize
     * @return mixed
     * @throws \yii\base\InvalidConfigException
     */
    public function getChangedSupplierInfoByTimeSlot($startDate, $endDate, $offset, $pageSize)
    {
        $sql = <<<sql
            SELECT supplier_id
            FROM b2b_company.uuc_supplier_boutique 
            WHERE supplier_id > :offset AND last_modified_time > :startdatetime AND last_modified_time < :enddatetime AND status = 2
            order by supplier_id ASC
            LIMIT :pageSize
sql;
        /** @var Connection $connection */
        $connection = \Yii::$app->get('db');
        $result = $connection->createCommand($sql)
            ->bindValue(':startdatetime', $startDate)
            ->bindValue(':enddatetime', $endDate)
            ->bindValue(':offset', $offset)
            ->bindValue(':pageSize', $pageSize)
            ->queryColumn();
        return $result;
    }

    /**
     * 通过公司ID返回供应商信息
     * @param string $companyName
     * @return mixed
     * @throws \yii\base\InvalidConfigException
     */
    public function getSupplierInfoByCompanyId($companyId = '')
    {
        $sql = <<<sql
                  select * from 
                      b2b_company.uuc_supplier_boutique 
                  where supplier_id = :supplier_id AND status = 2
sql;
        return \Yii::$app->get('db')
            ->createCommand($sql)
            ->bindValue(':supplier_id', $companyId)
            ->queryOne();
    }
}
